//
//  TstFirstChatVC.swift
//  TstChat
//
//  Created by wanghui on 2018/5/10.
//  Copyright © 2018年 daerwei. All rights reserved.
//

import UIKit
import Kingfisher

class SessionVC: ChatBaseController {
    
    lazy var tableView:UITableView = {
        let tableView = UITableView(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height), style: .plain)
        tableView.separatorStyle = .singleLine
        tableView.tableFooterView = UIView.init(frame: CGRect.zero)
        tableView.dataSource = (self as UITableViewDataSource)
        tableView.delegate = (self as UITableViewDelegate)
        tableView.register(TstFirstChatCell.self, forCellReuseIdentifier: "tstfirstchatcell")
        
        tableView.register(TstGroupchatCell.self, forCellReuseIdentifier: "TstGroupchatCell")
        tableView.register(TstGroupchatcellFortwo.self, forCellReuseIdentifier: "TstGroupchatcellFortwo")
        tableView.register(UINib(nibName: "TstGroupchatcellFortwo",bundle: nil), forCellReuseIdentifier: "TstGroupchatcellFortwo")
        tableView.register(UINib(nibName: "TstGroupchatcellForthree",bundle: nil), forCellReuseIdentifier: "TstGroupchatcellForthree")
        tableView.register(UINib(nibName: "TstGroupchatcellForFour",bundle: nil), forCellReuseIdentifier: "TstGroupchatcellForFour")
        tableView.register(UINib(nibName: "TstGroupchatcellForFive",bundle: nil), forCellReuseIdentifier: "TstGroupchatcellForFive")
        tableView.register(UINib(nibName: "TstGroupchatcellForsix",bundle: nil), forCellReuseIdentifier: "TstGroupchatcellForsix")
        tableView.register(UINib(nibName: "TstGroupchatcellForseven",bundle: nil), forCellReuseIdentifier: "TstGroupchatcellForseven")
        tableView.register(UINib(nibName: "TstGroupchatcellForeight",bundle: nil), forCellReuseIdentifier: "TstGroupchatcellForeight")
        tableView.register(UINib(nibName: "TstGroupchatcellFornine",bundle: nil), forCellReuseIdentifier: "TstGroupchatcellFornine")
        tableView.rowHeight = 70
        return tableView
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        notificationObserve()
        setUI()
    }
    
    let service = SessionService()
    var sessionList = [ChatSession]()
    
    func loadData() {
        DispatchQueue.global().async {
            self.sessionList = self.service.getAllRecentSessions()
            if self.sessionList.count <= 0 {
                return
            }
            var unreadcount:Int = 0
            self.sessionList.filter{ !$0.noDisturbing }.forEach{ unreadcount += $0.unreadCount }
            DispatchQueue.main.async {
                self.tableView.reloadData()
                if unreadcount > 0 {
                    self.tabBarItem.pp.addBadge(number: unreadcount, max: 99, maxStr: "99+")
                }else {
                    self.tabBarItem.pp.hiddenBadge()
                }
            }
        }
    }
    
    func notificationObserve() {
        // 添加通知监听
        NotificationCenter.default.addObserver(self, selector: #selector(getnewMsg(notification:)), name: Notification.Name("onNewMsg"), object: nil)
        
    }
    
    func setUI() {
        
        self.view.addSubview(tableView)
        self.view.backgroundColor = UIColor(named: "chat_bg")
        self.tableView.backgroundColor = UIColor(named: "chat_bg")
        
        let setupgroup = UIBarButtonItem(title: "建群", style: .plain, target: self, action:#selector(rightbuttonTap(button:)))
        self.navigationItem.rightBarButtonItems = [ setupgroup]
    }
    
    @objc func getnewMsg(notification: Notification) {
        loadData()
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    @objc func buttonTap(button:UIButton) {
        //        print("点击了首页按钮")
        self.tabBarController?.navigationController?.popViewController(animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @objc func rightbuttonTap(button:UIButton) {
        let vc = TstChooseContactVC();
        vc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

extension SessionVC: UITableViewDataSource, UITableViewDelegate{
    // datasource
    internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let session = sessionList[indexPath.row]
        if session.session.chatType == .p2p {
            let tstfirstchatcell = "tstfirstchatcell"
            let cell = tableView.dequeueReusableCell(withIdentifier: tstfirstchatcell, for: indexPath) as! TstFirstChatCell
            cell.nameLabel.text = session.sessionName
            cell.headimage.kf.setImage(with: Common.avatarUrl(session.avatar ?? "") ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.lastestnews.text = session.lastMsg?.msgText
            if let lastTime = session.lastMsg?.createTime {
                cell.latestTime.text = TimeUtil.instance.chatTimeStr(lastTime.timeIntervalSince1970)
            }
            //        cell.latestTime.text = TimeUtil.instance.chatTimeStr(session.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
            cell.nodisturbImage.isHidden = !session.noDisturbing
            if session.unreadCount > 0 {
                if cell.nodisturbImage.isHidden == true {
                    cell.headview.pp.addBadge(text: "\(session.unreadCount > 99 ? "99+" : "\(session.unreadCount)")")
                    cell.headview.pp.moveBadge(x: 0, y: 1)
                    cell.headview.pp.setBadge(flexMode: .head)
                    
                }else {
                    cell.headview.pp.addDot(color: .red)
                    cell.headview.pp.moveBadge(x: 0, y: 1)
                }
            } else {
                cell.headview.pp.hiddenBadge()
            }
            return cell
            
        } else {
            let group = self.sessionList[indexPath.row]
            let memberCount = group.avatars.count
            switch memberCount {
            case 1:
                let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatCell", for: indexPath) as! TstGroupchatCell
                let group = self.sessionList[indexPath.row]
                cell.nameLabel.text = group.sessionName
                cell.nodisturbImage.isHidden = !group.noDisturbing
                cell.lastestnews.text = group.lastMsg?.msgText
                if let lastTime = group.lastMsg?.createTime {
                    cell.latestTime.text = TimeUtil.instance.chatTimeStr(lastTime.timeIntervalSince1970)
                }
                //          cell.latestTime.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
                cell.headimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                
                if group.unreadCount > 0 {
                    if cell.nodisturbImage.isHidden == true {
                        cell.headview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                        cell.headview.pp.moveBadge(x: 0, y: 1)
                        cell.headview.pp.setBadge(flexMode: .head)
                    }else {
                        cell.headview.pp.addDot(color: .red)
                        cell.headview.pp.moveBadge(x: 0, y: 1)
                    }
                } else {
                    cell.headview.pp.hiddenBadge()
                }
                return cell
            case 2 :
                let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatcellFortwo", for: indexPath) as! TstGroupchatcellFortwo
                let group = self.sessionList[indexPath.row]
                cell.namelabel.text = group.sessionName
                cell.nodisturbingImgview.isHidden = !group.noDisturbing
                cell.lattestnewslabel.text = group.lastMsg?.msgText
                if let lastTime = group.lastMsg?.createTime {
                    cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(lastTime.timeIntervalSince1970)
                }
                //          cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
                
                cell.firstimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.secondimg.kf.setImage(with: Common.avatarUrl(group.avatars[1] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                
                if group.unreadCount > 0 {
                    if cell.nodisturbingImgview.isHidden == true {
                        cell.clearview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                        cell.clearview.pp.moveBadge(x: 0, y: 1)
                        cell.clearview.pp.setBadge(flexMode: .head)
                        
                    }else {
                        cell.clearview.pp.addDot(color: .red)
                        cell.clearview.pp.moveBadge(x: 0, y: 1)
                    }
                } else {
                    cell.clearview.pp.hiddenBadge()
                }
                return cell
            case 3 :
                let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatcellForthree", for: indexPath) as! TstGroupchatcellForthree
                let group = self.sessionList[indexPath.row]
                cell.namelabel.text = group.sessionName
                cell.nodisturbingImgview.isHidden = !group.noDisturbing
                cell.lattestnewslabel.text = group.lastMsg?.msgText
                if let lastTime = group.lastMsg?.createTime {
                    cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(lastTime.timeIntervalSince1970)
                }
                //          cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
                
                cell.firstimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.secondimg.kf.setImage(with: Common.avatarUrl(group.avatars[1] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.thirdimg.kf.setImage(with: Common.avatarUrl(group.avatars[2] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                if group.unreadCount > 0 {
                    if cell.nodisturbingImgview.isHidden == true {
                        cell.clearview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                        cell.clearview.pp.moveBadge(x: 0, y: 1)
                        cell.clearview.pp.setBadge(flexMode: .head)
                    }else {
                        cell.clearview.pp.addDot(color: .red)
                        cell.clearview.pp.moveBadge(x: 0, y: 1)
                    }
                }else {
                    cell.clearview.pp.hiddenBadge()
                }
                return cell
            case 4 :
                let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatcellForFour", for: indexPath) as! TstGroupchatcellForFour
                let group = self.sessionList[indexPath.row]
                cell.namelabel.text = group.sessionName
                cell.nodisturbingImgview.isHidden = !group.noDisturbing
                cell.lattestnewslabel.text = group.lastMsg?.msgText
                if let lastTime = group.lastMsg?.createTime {
                    cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(lastTime.timeIntervalSince1970)
                }
                //          cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
                cell.firstimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.secondimg.kf.setImage(with: Common.avatarUrl(group.avatars[1] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.thirdimg.kf.setImage(with: Common.avatarUrl(group.avatars[2] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.fourthimg.kf.setImage(with: Common.avatarUrl(group.avatars[3] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                if group.unreadCount > 0 {
                    if cell.nodisturbingImgview.isHidden == true {
                        cell.clearview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                        cell.clearview.pp.moveBadge(x: 0, y: 1)
                        cell.clearview.pp.setBadge(flexMode: .head)
                        
                    }else {
                        cell.clearview.pp.addDot(color: .red)
                        cell.clearview.pp.moveBadge(x: 0, y: 1)
                    }
                }else {
                    cell.clearview.pp.hiddenBadge()
                }
                return cell
            case 5 :
                let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatcellForFive", for: indexPath) as! TstGroupchatcellForFive
                let group = self.sessionList[indexPath.row]
                cell.namelabel.text = group.sessionName
                cell.nodisturbingImgview.isHidden = !group.noDisturbing
                cell.lattestnewslabel.text = group.lastMsg?.msgText
                if let lastTime = group.lastMsg?.createTime {
                    cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(lastTime.timeIntervalSince1970)
                }
                //          cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
                cell.firstimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.secondimg.kf.setImage(with: Common.avatarUrl(group.avatars[1] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.thirdimg.kf.setImage(with: Common.avatarUrl(group.avatars[2] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.fourthimg.kf.setImage(with: Common.avatarUrl(group.avatars[3] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.fifthimg.kf.setImage(with: Common.avatarUrl(group.avatars[4] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                if group.unreadCount > 0 {
                    if cell.nodisturbingImgview.isHidden == true {
                        cell.clearview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                        cell.clearview.pp.moveBadge(x: 0, y: 1)
                        cell.clearview.pp.setBadge(flexMode: .head)
                        
                    }else {
                        cell.clearview.pp.addDot(color: .red)
                        cell.clearview.pp.moveBadge(x: 0, y: 1)
                    }
                }else {
                    cell.clearview.pp.hiddenBadge()
                }
                return cell
            case 6 :
                let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatcellForsix", for: indexPath) as! TstGroupchatcellForsix
                let group = self.sessionList[indexPath.row]
                cell.namelabel.text = group.sessionName
                cell.nodisturbingImgview.isHidden = !group.noDisturbing
                cell.lattestnewslabel.text = group.lastMsg?.msgText
                if let lastTime = group.lastMsg?.createTime {
                    cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(lastTime.timeIntervalSince1970)
                }
                //          cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
                cell.firstimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.secondimg.kf.setImage(with: Common.avatarUrl(group.avatars[1] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.thirdimg.kf.setImage(with: Common.avatarUrl(group.avatars[2] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.fourthimg.kf.setImage(with: Common.avatarUrl(group.avatars[3] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.fifthimg.kf.setImage(with: Common.avatarUrl(group.avatars[4] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.sixthimg.kf.setImage(with: Common.avatarUrl(group.avatars[5] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                if group.unreadCount > 0 {
                    if cell.nodisturbingImgview.isHidden == true {
                        cell.clearview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                        cell.clearview.pp.moveBadge(x: 0, y: 1)
                        cell.clearview.pp.setBadge(flexMode: .head)
                        
                    }else {
                        cell.clearview.pp.addDot(color: .red)
                        cell.clearview.pp.moveBadge(x: 0, y: 1)
                    }
                }else {
                    cell.clearview.pp.hiddenBadge()
                }
                return cell
            case 7 :
                let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatcellForseven", for: indexPath) as! TstGroupchatcellForseven
                let group = self.sessionList[indexPath.row]
                cell.namelabel.text = group.sessionName
                cell.nodisturbingImgview.isHidden = !group.noDisturbing
                cell.lattestnewslabel.text = group.lastMsg?.msgText
                if let lastTime = group.lastMsg?.createTime {
                    cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(lastTime.timeIntervalSince1970)
                }
                //          cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
                cell.firstimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.secondimg.kf.setImage(with: Common.avatarUrl(group.avatars[1] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.thirdimg.kf.setImage(with: Common.avatarUrl(group.avatars[2] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.fourthimg.kf.setImage(with: Common.avatarUrl(group.avatars[3] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.fifthimg.kf.setImage(with: Common.avatarUrl(group.avatars[4] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.sixthimg.kf.setImage(with: Common.avatarUrl(group.avatars[5] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.sevenimg.kf.setImage(with: Common.avatarUrl(group.avatars[6] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                if group.unreadCount > 0 {
                    if cell.nodisturbingImgview.isHidden == true {
                        cell.clearview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                        cell.clearview.pp.moveBadge(x: 0, y: 1)
                        cell.clearview.pp.setBadge(flexMode: .head)
                        
                    }else {
                        cell.clearview.pp.addDot(color: .red)
                        cell.clearview.pp.moveBadge(x: 0, y: 1)
                    }
                }else {
                    cell.clearview.pp.hiddenBadge()
                }
                return cell
            case 8 :
                let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatcellForeight", for: indexPath) as! TstGroupchatcellForeight
                let group = self.sessionList[indexPath.row]
                cell.namelabel.text = group.sessionName
                cell.nodisturbingImgview.isHidden = !group.noDisturbing
                cell.lattestnewslabel.text = group.lastMsg?.msgText
                if let lastTime = group.lastMsg?.createTime {
                    cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(lastTime.timeIntervalSince1970)
                }
                //          cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
                cell.firstimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.secondimg.kf.setImage(with: Common.avatarUrl(group.avatars[1] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.thirdimg.kf.setImage(with: Common.avatarUrl(group.avatars[2] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.fourthimg.kf.setImage(with: Common.avatarUrl(group.avatars[3] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.fifthimg.kf.setImage(with: Common.avatarUrl(group.avatars[4] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.sixthimg.kf.setImage(with: Common.avatarUrl(group.avatars[5] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.sevenimg.kf.setImage(with: Common.avatarUrl(group.avatars[6] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.eighthimg.kf.setImage(with: Common.avatarUrl(group.avatars[7] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                if group.unreadCount > 0 {
                    if cell.nodisturbingImgview.isHidden == true {
                        cell.clearview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                        cell.clearview.pp.moveBadge(x: 0, y: 1)
                        cell.clearview.pp.setBadge(flexMode: .head)
                        
                    }else {
                        cell.clearview.pp.addDot(color: .red)
                        cell.clearview.pp.moveBadge(x: 0, y: 1)
                    }
                }else {
                    cell.clearview.pp.hiddenBadge()
                }
                return cell
            case 9 :
                let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatcellFornine", for: indexPath) as! TstGroupchatcellFornine
                let group = self.sessionList[indexPath.row]
                cell.namelabel.text = group.sessionName
                cell.nodisturbingImgview.isHidden = !group.noDisturbing
                cell.lattestnewslabel.text = group.lastMsg?.msgText
                if let lastTime = group.lastMsg?.createTime {
                    cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(lastTime.timeIntervalSince1970)
                }
                //          cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
                cell.firstimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.secondimg.kf.setImage(with: Common.avatarUrl(group.avatars[1] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.thirdimg.kf.setImage(with: Common.avatarUrl(group.avatars[2] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.fourthimg.kf.setImage(with: Common.avatarUrl(group.avatars[3] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.fifthimg.kf.setImage(with: Common.avatarUrl(group.avatars[4] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.sixthimg.kf.setImage(with: Common.avatarUrl(group.avatars[5] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.sevenimg.kf.setImage(with: Common.avatarUrl(group.avatars[6] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.eighthimg.kf.setImage(with: Common.avatarUrl(group.avatars[7] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.nineimg.kf.setImage(with: Common.avatarUrl(group.avatars[8] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                if group.unreadCount > 0 {
                    if cell.nodisturbingImgview.isHidden == true {
                        cell.clearview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                        cell.clearview.pp.moveBadge(x: 0, y: 1)
                        cell.clearview.pp.setBadge(flexMode: .head)
                        
                    }else {
                        cell.clearview.pp.addDot(color: .red)
                        cell.clearview.pp.moveBadge(x: 0, y: 1)
                    }
                }else {
                    cell.clearview.pp.hiddenBadge()
                }
                return cell
            default:
                let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatCell", for: indexPath) as! TstGroupchatCell
                let group = self.sessionList[indexPath.row]
                cell.nameLabel.text = group.sessionName
                cell.nodisturbImage.isHidden = !group.noDisturbing
                cell.lastestnews.text = group.lastMsg?.msgText
                if let lastTime = group.lastMsg?.createTime {
                    cell.latestTime.text = TimeUtil.instance.chatTimeStr(lastTime.timeIntervalSince1970)
                }
                //          cell.latestTime.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
                //          cell.headimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: UIImage.init(named: "defaultAvatar"),options: [.processor(CryptorProcessor())])
                cell.headimage.image = UIImage.init(named: "defaultAvatar")
                if group.unreadCount > 0 {
                    if cell.nodisturbImage.isHidden == true {
                        cell.headview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                        cell.headview.pp.moveBadge(x: 0, y: 1)
                        cell.headview.pp.setBadge(flexMode: .head)
                        
                    }else {
                        cell.headview.pp.addDot(color: .red)
                        cell.headview.pp.moveBadge(x: 0, y: 1)
                    }
                }else {
                    cell.headview.pp.hiddenBadge()
                }
                return cell
            }
        }
    }
    
    // delegate
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sessionList.count
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let session = sessionList[indexPath.row]
        let chatvc = ChatVC()
        chatvc.sessionName = session.sessionName
        chatvc.chatType = session.session.chatType
        chatvc.sessionServerId = session.session.sessionServerId
        chatvc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(chatvc, animated: true)
    }
    
    
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let deleteAction = UIContextualAction(style: .destructive, title: "删除") { (action, view, completionHandler) in
            self.alert(title: "删除会话", msg: "确认删除会话?", actions: [UIAlertAction(title: "取消", style: .cancel),
                                                                UIAlertAction(title: "删除", style: .destructive, handler: { [unowned self] action in
                let session = self.sessionList[indexPath.row]
                self.service.removeSession(sessionServerId: session.session.sessionServerId,chatType: session.session.chatType) { [unowned self] (success, msg) in
                    if !success {
                        self.view.makeToast("删除失败",position:.center)
                    }
                    self.sessionList.remove(at: indexPath.row)
                    self.tableView.deleteRows(at: [indexPath], with: .automatic)
                    completionHandler(success)
                }
            })])
        }
        deleteAction.backgroundColor = .red
        
        let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
        configuration.performsFirstActionWithFullSwipe = false
        
        return configuration
    }
    
}
